package com.are.servelet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.are.entidades.Avance;
import com.are.manejadores.ManejadorAvances;
import com.are.odisea.db;

/**
 * Servlet implementation class SrvAvances
 */
@WebServlet("/SrvAvances")
public class SrvAvances extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SrvAvances() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void ProcesarPeticion(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html;charset=ISO-8859-1");
    	PrintWriter out = response.getWriter();
    	String operacion = (String)request.getParameter("operacion");
    	HttpSession sesion = request.getSession();
		if (sesion.getAttribute("usuario") == null) {
			out.print("La sesion ha caducado... intente de nuevo por favor");
			return;
		}
		String usuario = (String)sesion.getAttribute("usuario");
    	
    	db conexion = null;
    	
    	try {
    		if (operacion.equals("add")) {
    			conexion = new db();
    			String avance = (String)request.getParameter("avance");
    			String orden = (String)request.getParameter("orden");
    			
    			ManejadorAvances MA = new ManejadorAvances(conexion);
    			
    			if (MA.Add(orden,usuario,avance)) {
    				out.print("OK");
    			}else {
    				out.print("Error al procesar la solicitud");
    			}
    		}
    		
    		if (operacion.equals("list")) {
    			String orden = (String)request.getParameter("orden");
    			ArrayList<Avance> lista = new ArrayList<Avance>();
    			ManejadorAvances MA = new ManejadorAvances(conexion);
    			lista = MA.List(orden);
    			
    			out.println("<h2>Avances</h2>");
    			out.println("<table>");
    			out.println("<tr>");
    			out.println("<th>Fecha</th>");
    			out.println("<th>Usuario</th>");
    			out.println("<th>Avance</th>");
    			out.println("</tr>");
    			for (Avance avance : lista) {
        			out.println("<tr>");
        			out.println("<td>" + avance.getFecha() + "</td>");
        			out.println("<td>" + avance.getUsuario() + "</td>");
        			out.println("<td>" + avance.getAvance() + "</td>");
        			out.println("</tr>");
    			}
    			out.println("<tr>");
    			out.println("<td colspan=3>Total avances: " + lista.size() + "</td>");
    			out.println("</tr>");
    			out.println("</table>");
    		}
    		
    		
    	}catch (SQLException e) {
    		out.println("Error con el Servidor " + e.getMessage());
    	}catch (Exception e) {
        		out.println("Excepcion: " + e.getMessage());
    	}finally {
    		if (conexion != null) {
    			try {
					conexion.Close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					out.println("Error al cerrar la conexion " + e.getMessage());
				}
    		}
    	}
    	
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcesarPeticion(request,
				response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		ProcesarPeticion(request,
				response);
	}

}
